package com.tetris.project;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.WindowManager;
//import com.android.music.IMediaPlaybackService;

public class Options extends PreferenceActivity {
	private static final String TAG = "Tetris";
	//private SharedPreferences mPrefs;
	private boolean next, sfx;
	public static boolean vibration;
	private CheckBoxPreference mMusicCheckBox, mVibCheckBox, mNextCheckBox, mSoundsEffects;
	final private String CB_PREFERENCES[] = {"music", "vibration", "next", "sounds"};
	SharedPreferences myPrefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.options);
		myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
			WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

		mMusicCheckBox = (CheckBoxPreference) findPreference(CB_PREFERENCES[0]);
		mMusicCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			@Override
			public boolean onPreferenceClick(Preference preference) {
				if (mMusicCheckBox.isChecked()) {
					Shr_Res.music = true;
					Log.d(TAG, "Play music");
				}
				else { 
					Shr_Res.music = false;
					Log.d(TAG, "Music stopped");
				}

				Log.d(Shr_Res.TAG, "Music check box changed?");

				
				if(Shr_Res.music && Shr_Res.screen) {
					Shr_Res.wellcomeMusic.play(R.raw.main);
				}

				if(!Shr_Res.music && Shr_Res.screen) {
					Shr_Res.wellcomeMusic.stop();
				}

				if(Shr_Res.music && !Shr_Res.screen) {
					Shr_Res.gameMusic.play(R.raw.game);
				}

				if(!Shr_Res.music && !Shr_Res.screen) {
					Shr_Res.gameMusic.stop();
				}
				return false;
				
				/*Intent i = new Intent();
				i.setClassName("com.android.music", "com.android.music.MediaPlaybackService");
				ServiceConnection conn = new MediaPlayerServiceConnection();
				bindService(i, conn, 0);
				return false;*/
			}
		});

        mVibCheckBox = (CheckBoxPreference) findPreference(CB_PREFERENCES[1]);
        mVibCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			@Override
			public boolean onPreferenceClick(Preference preference) {
				vibration = mVibCheckBox.isChecked();
				if (vibration){
					//TODO: Vibration ON
					Log.d(TAG, "Vibration ON");
				}
				else{
					//TODO: Vibration OFF
					Log.d(TAG, "Vibration OFF");
				}
					return false;
			}
		});
        
        mNextCheckBox = (CheckBoxPreference) findPreference(CB_PREFERENCES[2]);
        mNextCheckBox.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			@Override
			public boolean onPreferenceClick(Preference preference) {
				next = mNextCheckBox.isChecked();
				if (next){
					//TODO: Vibration ON
					Log.d(TAG, "Next Piece ON");
					Shr_Res.showNextPiece = true;
				}
				else{
					//TODO: Vibration OFF
					Log.d(TAG, "Next Piece OFF");
					Shr_Res.showNextPiece = false;
				}
				return false;
			}
		});
        
        mSoundsEffects = (CheckBoxPreference) findPreference(CB_PREFERENCES[3]);
        mSoundsEffects.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			@Override
			public boolean onPreferenceClick(Preference preference) {
				sfx = mSoundsEffects.isChecked();
				if (sfx){
					Shr_Res.sfx = true;
				}
				else{
					Shr_Res.sfx = false;
				}
				return false;
			}
		});
	}
	
	protected void onStart(){
		super.onStart();
		Shr_Res.icon = myPrefs.getString("icons", "spheres");
		Log.d(TAG, "Icons to use: "+Shr_Res.icon);
	}
	

	
	protected void onPause() {
		super.onPause();
		Shr_Res.icon = myPrefs.getString("icons", "spheres");
		Log.d(TAG, "Icons to use: "+Shr_Res.icon);
		//updateSettings();
	}
	
/*	public void updateSettings() {
		if ( vibration != mVibCheckBox.isChecked() ) {
			if ( mVibCheckBox.isChecked() ) {
				//TODO: Vibration ON
				Log.d(TAG, "Vibration ON");
			} else { 
				//TODO: Vibration OFF
				Log.d(TAG, "Vibration OFF");
			}
		}
		if ( next != mNextCheckBox.isChecked() ) {
			if ( mNextCheckBox.isChecked() ) {
				//TODO: Show next block
				Log.d(TAG, "Show next block");
			} else { 
				//TODO: Hide next block
				Log.d(TAG, "Hide next block");
			}
		}
		if (!sfx){
			Shr_Res.sfx = true;
		}
		else{
			Shr_Res.sfx = false;
		}
			
	}*/

/*	private class MediaPlayerServiceConnection implements ServiceConnection {
		public IMediaPlaybackService mService;

		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.i(TAG, "Connected! Name: " + name.getClassName());

			// This is the important line
			mService = IMediaPlaybackService.Stub.asInterface(service);

			// If all went well, now we can use the interface
			try {
				Log.i(TAG, "Playing track: " + mService.getTrackName());
				Log.i(TAG, "By artist: " + mService.getArtistName());
				if (mService.isPlaying()) {
					Log.i(TAG, "Music player is playing.");
				} else {
					Log.i(TAG, "Music player is not playing.");
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
			try {
			if(Shr_Res.music) {
					mService.openfile("/sdcard/TERNURA.mp3");
					mService.play();
			} else {
				mService.stop();
			}
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public void onServiceDisconnected(ComponentName name) {
			Log.i(TAG, "Disconnected!");
		}
	}*/
}
